package com.bosssoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibConferenceBudgetDTO;
import com.bosssoft.entity.dto.FibTrainBudgetDTO;
import com.bosssoft.entity.vo.FibConferenceBudgetVO;
import com.bosssoft.entity.vo.FibIndicatorsVO;
import com.bosssoft.entity.vo.FibTrainBudgetVO;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibConferenceBudgetService;
import com.bosssoft.service.FibTrainBudgetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.bosssoft.enums.BgtCode.CANT_FOUND_RECORD;

/**
 * @author Li kexi
 * Created on 2021/8/18.16:03
 */
@RestController
@Api(tags = "具体的支出预算控制器（会议费、培训费等）")
@RequestMapping("/little")
public class LittleBudgetController {

    @Autowired
    private FibConferenceBudgetService fibConferenceBudgetService;

    @Autowired
    private FibTrainBudgetService fibTrainBudgetService;

    // 会议费、培训费预算管理
    // 1、录入预算
    @PostMapping("/addConference")
    @ApiOperation(value = "录入会议预算")
    public Result addConference(@RequestBody @Validated FibConferenceBudgetVO request){

        FibConferenceBudgetDTO fibConferenceBudgetDTO = new FibConferenceBudgetDTO();
        BeanUtils.copyProperties(request,fibConferenceBudgetDTO);

         boolean save = fibConferenceBudgetService.addConference(fibConferenceBudgetDTO);
         if (!save){
             return Result.error();
         }
         return Result.ok();
    }

    @PostMapping("/addTrain")
    @ApiOperation(value = "录入培训预算")
    public Result addTrain(@RequestBody @Validated FibTrainBudgetVO request){

        FibTrainBudgetDTO fibTrainBudgetDTO = new FibTrainBudgetDTO();
        BeanUtils.copyProperties(request,fibTrainBudgetDTO);

        boolean save = fibTrainBudgetService.addTrain(fibTrainBudgetDTO);
        if (!save){
            return Result.error();
        }
        return Result.ok();
    }

    @PostMapping("/editConference")
    @ApiOperation("/修改此项会议预算")
    public Result editConference(@RequestBody @Validated FibConferenceBudgetVO request){

        FibConferenceBudgetDTO fibConferenceBudgetDTO = new FibConferenceBudgetDTO();
        BeanUtils.copyProperties(request,fibConferenceBudgetDTO);
        boolean edit = fibConferenceBudgetService.editConference(fibConferenceBudgetDTO);
        if (!edit){
            return Result.error(CANT_FOUND_RECORD);
        }
        return Result.ok();

    }

    @PostMapping("/editTrain")
    @ApiOperation("/修改此项培训预算")
    public Result editTrain(@RequestBody @Validated FibTrainBudgetVO request){

        FibTrainBudgetDTO fibTrainBudgetDTO = new FibTrainBudgetDTO();
        BeanUtils.copyProperties(request,fibTrainBudgetDTO);
        boolean edit = fibTrainBudgetService.editTrain(fibTrainBudgetDTO);
        if (!edit){
            return Result.error(CANT_FOUND_RECORD);
        }
        return Result.ok();

    }
    // 3、查询预算
    @PostMapping("/queryConference")
    @ApiOperation(value = "查询所有的会议预算")
    public Result queryConference(@RequestParam(defaultValue = "1")Integer pageNo,
                                  @RequestParam(defaultValue = "10")Integer pageSize,
                                  FibConferenceBudgetVO fibConferenceBudgetVO){

        Page<FibConferenceBudgetVO> page = fibConferenceBudgetService.selectConference(fibConferenceBudgetVO,pageNo,pageSize);
        PageResult<FibConferenceBudgetVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page",pageResult);
    }

    @PostMapping("/queryTrain")
    @ApiOperation(value = "查询所有的培训预算")
    public Result queryTrain(@RequestParam(defaultValue = "1")Integer pageNo,
                                              @RequestParam(defaultValue = "10")Integer pageSize){

        Page<FibTrainBudgetVO> page = fibTrainBudgetService.selectTrain(pageNo,pageSize);
        PageResult<FibTrainBudgetVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page",pageResult);
    }


    @DeleteMapping("/deleteConference")
    @ApiOperation("删除会议预算")
    public Result deleteConference(Long id){
        boolean b = fibConferenceBudgetService.removeById(id);
        if (!b){
            return Result.error();
        }
        return  Result.ok();
    }

    @DeleteMapping("/deleteTrain")
    @ApiOperation("删除培训预算")
    public Result deleteTrain(Long id){
        boolean b = fibTrainBudgetService.removeById(id);
        if (!b){
            return Result.error();
        }
        return  Result.ok();
    }

}
